package com.dongdongshop.service.impl;

import com.dongdongshop.mapper.TbSpecificationMapper;
import com.dongdongshop.mapper.TbSpecificationOptionMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbSpecificationOptionSerive;
import com.dongdongshop.vo.TbSpecificationOptionVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@DubboService
public class TbSpecificationOptionServiceImpl implements TbSpecificationOptionSerive {

    //注入mapper
    @Autowired
    private TbSpecificationOptionMapper tbSpecificationOptionMapper;
    @Autowired
    private TbSpecificationMapper tbSpecificationMapper;

    //查询规格全部数据(要求分页)
    @Override
    public PageResult<TbSpecification> TbSpecificationshowall(Integer pageNum, Integer pageSize, String specName) {
        PageHelper.startPage(pageNum, pageSize);
        List<TbSpecification> tbSpecifications = null;
        if ("".equals(specName) || specName == null) {
            tbSpecifications = tbSpecificationMapper.selectByExample(null);
        } else {
            TbSpecificationExample te = new TbSpecificationExample();
            TbSpecificationExample.Criteria criteria = te.createCriteria();
            criteria.andSpecNameLike("%" + specName + "%");
            tbSpecifications = tbSpecificationMapper.selectByExample(te);
        }
        PageInfo<TbSpecification> info = new PageInfo(tbSpecifications);
        return new PageResult<TbSpecification>(pageNum, pageSize, info.getTotal(), info.getList(), info.getPages());
    }

    //添加规格数据
    @Transactional//事务注解
    @Override
    public void addTbSpecification(TbSpecification specName, List<TbSpecificationOption> tbSpecificationOptions) {
        tbSpecificationMapper.insertSelective(specName);
        for (int i = 0; i < tbSpecificationOptions.size(); i++) {
            tbSpecificationOptions.get(i).setSpecId(specName.getId());
            tbSpecificationOptionMapper.insertSelective(tbSpecificationOptions.get(i));
        }
    }

    //规格数据回显
    @Override
    public TbSpecificationOptionVO showecho(Long id) {
        TbSpecification tbSpecification = tbSpecificationMapper.selectByPrimaryKey(id);
        TbSpecificationOptionExample te = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = te.createCriteria();
        criteria.andSpecIdEqualTo(id);
        List<TbSpecificationOption> tbSpecificationOptions = tbSpecificationOptionMapper.selectByExample(te);
        TbSpecificationOptionVO vo = new TbSpecificationOptionVO(tbSpecification.getId(),tbSpecification.getSpecName(),tbSpecificationOptions);
        return vo;
    }

    //修改规格数据
    @Transactional//事务注解
    @Override
    public void updateTbSpecification(TbSpecification specName, List<TbSpecificationOption> tbSpecificationOptions) {
        tbSpecificationMapper.updateByPrimaryKeySelective(specName);
        TbSpecificationOptionExample te = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = te.createCriteria();
        criteria.andSpecIdEqualTo(specName.getId());
        tbSpecificationOptionMapper.deleteByExample(te);
        for (int i = 0; i < tbSpecificationOptions.size(); i++) {
            tbSpecificationOptions.get(i).setSpecId(specName.getId());
            tbSpecificationOptionMapper.insertSelective(tbSpecificationOptions.get(i));
        }
    }

    //删除单个数据
    @Transactional//事务注解
    @Override
    public void deleteTbSpecification(Long id) {
        tbSpecificationMapper.deleteByPrimaryKey(id);
        TbSpecificationOptionExample te = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = te.createCriteria();
        criteria.andSpecIdEqualTo(id);
        tbSpecificationOptionMapper.deleteByExample(te);
    }

    //删除多个数据
    @Transactional//事务注解
    @Override
    public void deleteallTbSpecification(List<Long> ids) {
        TbSpecificationExample tee = new TbSpecificationExample();
        TbSpecificationExample.Criteria criteria1 = tee.createCriteria();
        criteria1.andIdIn(ids);
        tbSpecificationMapper.deleteByExample(tee);
        TbSpecificationOptionExample te = new TbSpecificationOptionExample();
        TbSpecificationOptionExample.Criteria criteria = te.createCriteria();
        criteria.andSpecIdIn(ids);
        tbSpecificationOptionMapper.deleteByExample(te);
    }

    //查询规格全部数据
    @Override
    public List<TbSpecification> TbSpecificationshow() {
        return tbSpecificationMapper.selectByExample(null);
    }
}
